package map_set;

import java.util.HashMap;
import java.util.Map;

//138. 复制带随机指针的链表
class Node {
    int val;
    Node next;
    Node random;

    public Node(int val) {
        this.val = val;
        this.next = null;
        this.random = null;
    }
}

public class Num138_copyRandomList {
    public Node copyRandomList(Node head) {
        if (head==null){
            return null;
        }
        //Map
        Map<Node,Node> map = new HashMap<>();
        for (Node x=head ;x!=null ; x=x.next) {
            Node node = new Node(x.val);
            map.put(x,node);
        }
        for (Node x=head ;x!=null ; x=x.next) {
            map.get(x).next=map.get(x.next);
            map.get(x).random=map.get(x.random);

        }
        return map.get(head);

    }
}
